package TUILayer;//makes this class a part of the TUILayer package
import ControlLayer.*;
import ModelLayer.*;
import java.util.*;

/**
 * 
 * User interface - Main Menu
 * <p>
 * <code>MainMenuUI</code> class manages the entry-point of the application, as it redirects the user to the desired submenu.
 * <br>
 * After calling the <code>mainMenuStart()</code> method, User Interface is displayed in the <code>Terminal</code> and accepts input data from the user, who can do various tasks related to managing DVDs, AddressBook with friends and Loans.
 * @author (dmaj0913)
 * @version (2013.11.08)
 * <p>
 * <b><i>History:</i></b>
 * <p>
 * (KBH): 2005.03.31:
 * <p>
 * (FEN): 2006.02.24: Comments translated into English
 * <p>
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering and loose coupling
 *  <p>                  
 * (KBH): 2008.10.24: Change af manimenuStart to if
 * 
 */
public class MainMenuUI
{
    //the reference to the ControlLayer
    private AddressBookUI adrUI;//references to other UI objects
    private DvdUI dvdUI;
    private LoanUI loanUI;
    private ArrayList<DVD> dvdMemoryBackup;
    private ArrayList<Loan> loanMemoryBackup;
    private AddressBook addressBookMemoryBackup;
    private boolean menu = false;
   
    /**
     * Constructor creates new instance of <code>Addressbook</code> class and new empty collections of DVDs and Loans.
     */
    public MainMenuUI()
    {
        dvdMemoryBackup = new ArrayList<DVD>();
        addressBookMemoryBackup = new AddressBook();
        loanMemoryBackup = new ArrayList<Loan>();
    }
    /**
     * Manages the input data and redirects user to desired submenu using other methods in the class.
     */
    public void mainMenuStart()
    {
        boolean exit = false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            if(dvdMemoryBackup.size() > 0 && addressBookMemoryBackup.getPersons().size() > 0) menu = true;
            int choice = writeMainMenu();
            if(choice == 1)
            {
                startAddressBook();
            }
            else{
                    if(choice == 2){ 
                        startDvd();
                    }
                    else{
                        if(choice == 3){
                            if(menu == true)  startLoan(); 
                            }
                        else{
                            writeEnd();
                            exit = true;
                        }//end else
                    }// end else
             }//end else
        }//end while
    }//end start
    /**
    * Accepts input data and prints information in the Terminal.
    * @return number of type <code>Int</code> as a desired choice.
    */
    private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** MainMenu ***");
            System.out.println(" (1) AddressBook");
            System.out.println(" (2) DVDs");
            System.out.println(" (3) Loan");
            System.out.println(" (4) Close");
            if(menu == false) System.out.println(" NOTE: You require at least one person and at least one copy to exist before registering a loan.");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    /**
     * Starts the User Interface submenu for managing an Addressbook with friends. Creates a backup of the collection of friends.
     */
    private void startAddressBook()
    {
        adrUI= new AddressBookUI();
        addressBookMemoryBackup = adrUI.start(addressBookMemoryBackup);    
    }
    /**
     * Starts the User Interface submenu for managing DVDs. Creates a backup of the collection of DVDs.
     */
    private void startDvd()
    {
        dvdUI = new DvdUI();
        dvdMemoryBackup = dvdUI.start(dvdMemoryBackup);
    }
    /**
     * Starts the User Interface submenu for managing Loans. Creates a backup of the collection of Loans.
     */
    private void startLoan()
    {
        loanUI = new LoanUI(addressBookMemoryBackup, dvdMemoryBackup);
        loanMemoryBackup = loanUI.start(loanMemoryBackup);
    }
    /**
     * Prints out a message in the Terminal, that the System is shutting down.
     */
    private void writeEnd()
    {
        System.out.println(" The System is shuting down ");
    }
 }
